<style>

</style>
<template>
  <div id="sg_goods">
    <goodsDetail :product="product" :shop="shop" :evalData="evalData" :evalDataSome="evalDataSome" :evalArray="evalArray" :favorId="favorId" :isCollect="isCollect" :promotionsData="promotions" :collectState="collectState">
    </goodsDetail>
  </div>
</template>
<script>
import Vue from "vue";
import goodsDetail from "components/goods/goodsDetail";
// service
import QueryProductReviewService from "services/seeGoods/QueryProductReview";
import QueryProductInfoService from "services/seeGoods/QueryProductInfo";

export default {
  data() {
    return {
      product: {}, // 产品参数对象
      shop: {}, // 商家信息参数对象
      evalData: {}, // 评价参数对象
      evalDataSome: {}, // 列表默认展示 三条评价
      evalArray: [], // 评价数组
      isCollect: true, // 是否收藏
      favorId: "", // 收藏id
      collectState: "收藏",
      promotions: [] //促销
    };
  },
  beforeRouteEnter(to, from, next) {
    Promise.all([
      queryProductInfo(Vue.prototype, to),
      queryProductReview(Vue.prototype, to)
    ]).then(results => {
      next(vm => {
        // 判断是否是微信环境
        if (vm.$.env==="webOrWechat") {
          // 微信环境, 直接跳转到app下载页
          vm.$router.replace({
            name: 'down_page'
          })
          return 
        }
        
        // 产品
        vm.product = results[0].product;
        console.log("vm.product: ", vm.product);
        vm.promotions = results[0].promotions;
        vm.shop = results[0].shop;
        vm.favorId = results[0].product.favorId;

        if (vm.product.isFavor === "1") {
          vm.isCollect = false;
          vm.collectState = "已收藏";
        } else {
          vm.isCollect = true;
          vm.collectState = "收藏";
        }

        // 评价
        vm.evalData = results[1];
        vm.evalDataSome = results[1];
        vm.evalDataSome.reviews = vm.evalDataSome.reviews.slice(0, 2);
        vm.evalArray = [
          { text: "全部", num: vm.evalData.all, active: true, type: 0 },
          { text: "好评", num: vm.evalData.positive, active: false, type: 1 },
          { text: "中评", num: vm.evalData.moderate, active: false, type: 2 },
          { text: "差评", num: vm.evalData.negative, active: false, type: 3 },
          { text: "有图", num: vm.evalData.imageCount, active: false, type: 4 }
        ];
      });
    });
  },
  components: {
    goodsDetail
  }
};
// 查看商品接口
function queryProductInfo(vm, to) {
  return new Promise(function(resolve, reject) {
    QueryProductInfoService.bind(vm)({
      productId: to.params.id
    },
    res => {
      resolve(res);
    });
  });
}

// 商品评价列表接口
function queryProductReview(vm, to) {
  return new Promise(function(resolve, reject) {
    QueryProductReviewService.bind(vm)({
      type: "0",
      index: "0",
      count: "10",
      productId: to.params.id
    },
    res => {
      resolve(res);
    });
  });
}
</script>
